package com.quectel.core.module.group.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.core.module.device.dto.DeviceTypeDto;
import com.quectel.core.module.device.service.DeviceTypeService;
import com.quectel.core.module.group.dao.SysGroupDeviceTypeDao;
import com.quectel.core.module.group.dto.SysGroupDeviceTypeDto;
import com.quectel.core.module.group.entity.SysGroupDeviceTypeEntity;
import com.quectel.core.module.group.service.SysGroupDeviceTypeService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 12:14:05
 */
@DubboService
public class SysGroupDeviceTypeServiceImpl implements SysGroupDeviceTypeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysGroupDeviceTypeServiceImpl.class);

    @Autowired
    private SysGroupDeviceTypeDao sysGroupDeviceTypeDao;
    @Autowired
    private DeviceTypeService deviceTypeService;

    @Override
    public void deleteByGroupId(Long groupId) {
        sysGroupDeviceTypeDao.delete(new QueryWrapper<SysGroupDeviceTypeEntity>().eq("sys_group_id", groupId));
    }

    @Override
    public List<DeviceTypeDto> selectByGroupId(Long groupId) {
        List<DeviceTypeDto> result = new ArrayList<>();

        List<SysGroupDeviceTypeEntity> groupDeviceTypeEntityList =
                sysGroupDeviceTypeDao.selectList(new QueryWrapper<SysGroupDeviceTypeEntity>().eq("sys_group_id", groupId));
        if (groupDeviceTypeEntityList != null && groupDeviceTypeEntityList.size() > 0) {
            for (SysGroupDeviceTypeEntity entity : groupDeviceTypeEntityList) {
                DeviceTypeDto deviceTypeDto = deviceTypeService.selectCacheById(entity.getDeviceTypeId());
                if (deviceTypeDto != null) {
                    result.add(deviceTypeDto);
                }
            }
        }
        return result;
    }

    @Override
    public List<SysGroupDeviceTypeDto> queryList(Map<String, Object> params) {
        List<SysGroupDeviceTypeEntity> list = sysGroupDeviceTypeDao.queryList(params);
        return CopyUtils.copyList(list, SysGroupDeviceTypeDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return sysGroupDeviceTypeDao.queryTotal(params);
    }

    @Override
    public Long save(SysGroupDeviceTypeDto dto) {
        SysGroupDeviceTypeEntity entity = CopyUtils.copyObj(dto, SysGroupDeviceTypeEntity.class);
        entity.setId(Snowflake.nextId());
        sysGroupDeviceTypeDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SysGroupDeviceTypeDto dto) {
        SysGroupDeviceTypeEntity entity = CopyUtils.copyObj(dto, SysGroupDeviceTypeEntity.class);
        sysGroupDeviceTypeDao.updateById(entity);
    }

    @Override
    public SysGroupDeviceTypeDto selectById(Long id) {
        SysGroupDeviceTypeEntity entity = sysGroupDeviceTypeDao.selectById(id);
        return CopyUtils.copyObj(entity, SysGroupDeviceTypeDto.class);
    }

    @Override
    public void deleteById(Long id) {
        sysGroupDeviceTypeDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
